package com.zzx.work1;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("cars")
@SessionAttributes(names= {"cars"},types= {ArrayList.class})
public class CarsController {

private static List<Cars> cars=new ArrayList<>();
	
	static{
		cars.add(new Cars(100000.0,"奇瑞QQ",1.5,400,"红色"));
		cars.add(new Cars(200000.0,"桑塔纳",1.5,300,"黑色"));
		cars.add(new Cars(500000.0,"保时捷",1.5,600,"黄色"));
	}
	
	@RequestMapping("all")
	public String findAll(Map<String, Object> map) {
		map.put("cars", cars);
		return "cs/cars";
	}
	
	@RequestMapping("del")
	public String del(@RequestParam("brand") String brand) {
		cars.remove(new Cars(0,brand,0,0,null));
		return "redirect:all";//response.sendRedirect()
	}
	
	@RequestMapping("add")
	public String add() {
		return "addcar";
	}
	
	@RequestMapping("doadd")
	public String doadd(Cars car) {
		System.out.println(car.getPrice()+car.getBrand()+car.getDisplacement()+car.getSoup()+car.getColor());
		cars.add(car);
		return "redirect:all";
	}
	
	@RequestMapping("update")
	public String update() {
		return "updatecar";
	}
	
	@RequestMapping("doupdate")
	public String doupdate(@RequestParam("index") String brand,Cars car) {
		int indexOf = cars.indexOf(new Cars(0,brand,0,0,null));
		if(indexOf != -1) {
			cars.set(indexOf, car);
		}
		System.out.println(car.getPrice()+car.getBrand()+car.getDisplacement()+car.getSoup()+car.getColor());
		return "redirect:all";
	}
	
}
